<template>
  <el-dialog title="导出" :close-on-click-modal="false" :visible.sync="visible"
    class="JNPF-dialog JNPF-dialog_center JNPF-dialog-export" lock-scroll width="600px">
    <el-form label-position="top">
      <el-form-item label='导出方式'>
        <p slot="label" class="export-label">导出方式</p>
        <el-radio-group v-model="type" @input="changeType">
          <template v-if="!isOrg">
            <el-radio :label="0">当前页面数据</el-radio>
            <el-radio :label="1">全部页面数据</el-radio>
          </template>
          <template v-else>
            <el-radio :label="0">公司和部门</el-radio>
            <el-radio :label="1">公司</el-radio>
            <el-radio :label="2">部门</el-radio>
          </template>
        </el-radio-group>
      </el-form-item>
      <el-form-item class="export-line">
        <p slot="label" class="export-label">导出数据<span>请选择导出字段</span></p>
        <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll"
          @change="handleCheckAllChange">全选</el-checkbox>
        <el-checkbox-group v-model="columns" @change="handleCheckedChange">
          <el-checkbox v-for="item in columnList" :label="item.prop" :key="item.prop"
            class="column-item" :title="item.label">
            {{item.label}}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <p class="footer-tip">提示:系统将导出列表中选中的数据</p>
      <el-button @click="visible=false">{{$t('common.cancelButton')}}</el-button>
      <el-button type="primary" @click="downLoad">导 出</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { noGroupList } from '@/components/Generator/generator/comConfig'
export default {
  data() {
    return {
      isOrg:false,
      visible: false,
      btnLoading: false,
      type: 0,
      columns: [],
      checkAll: true,
      isIndeterminate: false,
      columnList: [],
      selectIds: [],
      webType: '',
      exportData:[
        [
          { label: '类型', prop: 'category', width: 150 },
          { label: '所属组织', prop: 'parentId', width: 150 },
          { label: '名称', prop: 'fullName', width: 150 },
          { label: '编码', prop: 'enCode', width: 150 },
          { label: '公司简称', prop: 'shortName', width: 150 },
          { label: '公司性质', prop: 'enterpriseNature', width: 150 },
          { label: '所属行业', prop: 'industry', width: 150 },
          { label: '成立时间', prop: 'foundedTime', width: 150 },
          { label: '公司电话', prop: 'telePhone', width: 150 },
          { label: '公司传真', prop: 'fax', width: 150 },
          { label: '公司主页', prop: 'webSite', width: 150 },
          { label: '公司地址', prop: 'address', width: 150 },
          { label: '公司法人', prop: 'managerName', width: 150 },
          { label: '联系电话', prop: 'managerTelePhone', width: 150 },
          { label: '联系手机', prop: 'managerMobilePhone', width: 150 },
          { label: '联系邮箱', prop: 'manageEmail', width: 150 },
          { label: '开户银行', prop: 'bankName', width: 150 },
          { label: '银行账户', prop: 'bankAccount', width: 150 },
          { label: '经营范围', prop: 'businessscope', width: 150 },
          { label: '部门主管', prop: 'managerId', width: 150 },
          { label: '排序', prop: 'sortCode', width: 150 },
          { label: '说明', prop: 'description', width: 150 },
        ],
        [
          { label: '类型', prop: 'category', width: 150 },
          { label: '上级公司', prop: 'parentId', width: 150 },
          { label: '公司名称', prop: 'fullName', width: 150 },
          { label: '公司编码', prop: 'enCode', width: 150 },
          { label: '公司简称', prop: 'shortName', width: 150 },
          { label: '公司性质', prop: 'enterpriseNature', width: 150 },
          { label: '所属行业', prop: 'industry', width: 150 },
          { label: '成立时间', prop: 'foundedTime', width: 150 },
          { label: '公司电话', prop: 'telePhone', width: 150 },
          { label: '公司传真', prop: 'fax', width: 150 },
          { label: '公司主页', prop: 'webSite', width: 150 },
          { label: '公司地址', prop: 'address', width: 150 },
          { label: '公司法人', prop: 'managerName', width: 150 },
          { label: '联系电话', prop: 'managerTelePhone', width: 150 },
          { label: '联系手机', prop: 'managerMobilePhone', width: 150 },
          { label: '联系邮箱', prop: 'manageEmail', width: 150 },
          { label: '开户银行', prop: 'bankName', width: 150 },
          { label: '银行账户', prop: 'bankAccount', width: 150 },
          { label: '经营范围', prop: 'businessscope', width: 150 },
          { label: '排序', prop: 'sortCode', width: 150 },
          { label: '说明', prop: 'description', width: 150 },
        ],
        [
          { label: '类型', prop: 'category', width: 150 },
          { label: '所属组织', prop: 'parentId', width: 150 },
          { label: '部门名称', prop: 'fullName', width: 150 },
          { label: '部门编码', prop: 'enCode', width: 150 },
          { label: '部门主管', prop: 'managerId', width: 150 },
          { label: '排序', prop: 'sortCode', width: 150 },
          { label: '说明', prop: 'description', width: 150 },
        ],
      ]
    }
  },
  methods: {
    changeType(val){
      if(!this.isOrg) return
      let columnList = this.exportData[val]
      this.columnList = columnList.filter(o => {
        if(o.__config__) return !noGroupList.includes(o.__config__.jnpfKey)
        return !noGroupList.includes(o.id)
      })
      this.columns =  this.columnList.map(o => o.prop)
    },
    init(columnList, selectIds, webType,isOrg=false) {
      this.webType = webType || ''
      this.visible = true
      this.checkAll = true
      this.isIndeterminate = false
      this.isOrg = isOrg
      if(isOrg) columnList = this.exportData[0]
      columnList.forEach(item=>item.label = item.label.replace('*',''))
      columnList = JSON.parse(JSON.stringify(columnList))
      this.columnList = columnList.filter(o => {
        if(o.__config__) return !noGroupList.includes(o.__config__.jnpfKey)
        return !noGroupList.includes(o.id)
      })
      this.selectIds = selectIds || []
      this.columns = columnList.map(o => o.prop)
    },
    handleCheckAllChange(val) {
      this.columns = val ? this.columnList.map(o => o.prop) : [];
      this.isIndeterminate = false;
    },
    handleCheckedChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.columnList.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.columnList.length;
    },
    downLoad() {
      if (!this.columns.length) return this.$message.warning(`请至少选择一个导出字段`)
      this.$emit('download', { dataType: this.type, selectKey: this.columns, selectIds: this.selectIds })
    }
  }
}
</script>
